﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Threading;

namespace PepsiTwitterFeed
{
    public partial class Form1 : Form
    {
        string m_ConnectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa ; Password=sa";
        public Form1()
        {
            InitializeComponent();
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Bt_stop.Enabled = true;
            Bt_Start.Enabled = false;

            Thread pepsiFeed = new Thread(new ThreadStart(GetFeed));
            pepsiFeed.Start();
        }

        private void GetFeed()
        {
            while (!Bt_Start.Enabled)
            {
                string updatedFeed = "[Color Val=\"#FFFFFF\"]";

                try
                {
                    XmlDocument xdoc = new XmlDocument();

                    xdoc.Load("http://a.wtx.me/customtour/pepsitweet12/ssn.asp?numchars=1100");

                    XmlNodeList nList = xdoc.SelectNodes("/SSDATA/MSG");



                    string miniSpace = "    ";

                    foreach (XmlNode msg in nList)
                    {
                        string msgText = "[Font Val=\"Futura Md\"]" + miniSpace + msg.ChildNodes[0].InnerText;
                        string author = "[Font Val=\"FuturaStd-Bold\"]" + miniSpace + msg.ChildNodes[1].InnerText;

                        updatedFeed += miniSpace + "[IMAGETAG ID=\"688\"]" + author + msgText;
                    }
                }
                catch (Exception e)
                {
                    updatedFeed = "[Color Val=\"#FFFFFF\"]    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @samgnapp[Font Val=\"Futura Md\"]    #NickiMinajConcert aug 14 can't wait !!! Ahhhhhh    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @NiallsFeesh[Font Val=\"Futura Md\"]    Cant wait to see the live stream for the #nickiminajconcert 8    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @allymacaulay[Font Val=\"Futura Md\"]    I sooooo wannna go to the #NickiMinajConcert     [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @rockinblonde_em[Font Val=\"Futura Md\"]    i feel like a #NickiMinajConcert would be like an insane party    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @O5_13_2O12Dopee[Font Val=\"Futura Md\"]    You know what sounds great for today a #NickiMinajConcert    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @Yasmineluvpeace[Font Val=\"Futura Md\"]    I want to go to #NickiMinajConcert so bad!!    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @GeorginaLovatic[Font Val=\"Futura Md\"]    #NickiMinajConcert Boom Boom Boom Super Bass Baby:)    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @Smiles4Ever143[Font Val=\"Futura Md\"]    want to c the #NickiMinajConcert !!!!!!!!    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @usernine[Font Val=\"Futura Md\"]    Where can I buy all that cool stuff associated with ABC shows?    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @iLead_SheFollow[Font Val=\"Futura Md\"]    Cant Wait For The #NickiMinajConcert #PepsiRSVP ! Tuesday !!!    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @JustiziAa[Font Val=\"Futura Md\"]    #NickiMinajConcert id like to be there    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @SaidHeNeverBeen[Font Val=\"Futura Md\"]    Finishing up getting ready and then meeting up with my boo @shells_bythesea in NY ! #NickiMinajConcert @NICKIMINAJ !    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @PRINCEKEN0[Font Val=\"Futura Md\"]    Cant Wait For The #NickiMinajConcert ! Im Hoping To Meet @NICKIMINAJ While Im There #TeamNicki";

                }
                finally
                {
                   // updatedFeed = "[Color Val=\"#FFFFFF\"]    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @pepsi[Font Val="Futura Md"]    One more reason to love today: Our #NickiMinajConcert livestream on http://pepsi.com/ is on at 9:30pm ET! #dontforget    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @JustiziAa[Font Val="Futura Md"]    #NickiMinajConcert id like to be there    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @CBwifey_11[Font Val="Futura Md"]    Going 2 a #NickiMinajConcert would b AMAZING!!!!! :-)    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @MellzMystro[Font Val="Futura Md"]    I'm sorry I can't miss the #NickiMinajConcert live stream !!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Jauan_Levis514[Font Val="Futura Md"]    #NickiMinajConcert is on my bucket list    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @xoxoEnomis_[Font Val="Futura Md"]    #NickiMinajConcert , I Mustt GO :)    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @ItsChelsBitchhh[Font Val="Futura Md"]    I wanna go to a #NickiMinajConcert    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @SCAFFBEEZYjr[Font Val="Futura Md"]    @NICKIMINAJ  i just cant wait till Tuesday #NickiMinajConcert livestream!!!!!!!!!!!!! :P    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @charjacobs1[Font Val="Futura Md"]    I wanna go to the #NickiMinajConcert    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Meeyah143[Font Val="Futura Md"]    If i went to #NickiMinajConcert I would pass out. I love Nicki Minaj she is so dope.    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @furgee221[Font Val="Futura Md"]    hot day   ice cold pepsi = best #summer ever  #LiveForNow    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @sllewyma[Font Val="Futura Md"]    cant wait to view the #NickiMinajConcert !!!!!!!!!!!!!!!!!!!!!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Retroo_LOVEE[Font Val="Futura Md"]    #NickiMinajConcert  i wanna go!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @katiediseroad[Font Val="Futura Md"]    #NickiMinajConcert is trending OH MY GOSH i wanna goooo!";
                }


                UpdateToDB(updatedFeed);
                Thread.Sleep(180000);
            }
        }

        private void UpdateToDB(string dataTagInfo)
        {
            try
            {
                SqlConnection conn = new SqlConnection(m_ConnectionString);

                conn.Open();

                string sqlcmd = "update DataTag set LastValue = '" + dataTagInfo.Replace("'", "''") + "' where Name = '[PepsiTweet]'";

    

                SqlCommand cmd = new SqlCommand(sqlcmd, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
            }
            
        }

        private void Bt_stop_Click(object sender, EventArgs e)
        {
            Bt_Start.Enabled = true;
 
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Bt_Start.Enabled = true;
        }


    }
}
